package
{
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;

	public class RatingBox extends FlxGroup
	{
		private const CONTAINER_WIDTH:int = 300;
		private const CONTAINER_HEIGHT:int = 80;
		private var container:FlxSprite;
		private var starOne:FlxSprite;
		private var starTwo:FlxSprite;
		private var starThree:FlxSprite;
		private var starFour:FlxSprite;
		private var starFive:FlxSprite;
		private var none:FlxText;
		private var checkmark:FlxSprite;
		
		private var wasUpdated:Boolean;
		private var ratingValue:int;
		
		public function RatingBox(x:Number, y:Number)
		{
			container = new FlxSprite();
			container.makeGraphic(CONTAINER_WIDTH, CONTAINER_HEIGHT);
			container.drawRect(0, 0, CONTAINER_WIDTH, CONTAINER_HEIGHT, 0x000000, 1);
			container.x = x;
			container.y = y;
			add(container);
			
			var title:FlxText = new FlxText(container.x + 10, container.y + 10, container.width - 20, "Rate this Puzzle", false);
			title.setFormat("Courier New", 24, 0xFFFFFF, "left", 1, true);
			add(title);
			
			starOne = new FlxSprite(container.x + 10, title.y + title.height, Res.StarEmpty);
			starTwo = new FlxSprite(starOne.x + 35, starOne.y, Res.StarEmpty);
			starThree = new FlxSprite(starTwo.x + 35, starOne.y, Res.StarEmpty);
			starFour = new FlxSprite(starThree.x + 35, starOne.y, Res.StarEmpty);
			starFive = new FlxSprite(starFour.x + 35, starOne.y, Res.StarEmpty);
			
			none = new FlxText(starFive.x + 35, starOne.y + 10, 80, "( none )", false);
			title.setFormat("Courier New", 20, 0xFFFFFF, "left", 1);
			add(none);
			
			add(starOne);
			add(starTwo);
			add(starThree);
			add(starFour);
			add(starFive);
			
			checkmark = new FlxSprite(container.x + container.width - 50, container.y + 30, Res.Checkmark);
			
			ratingValue = 0;
			wasUpdated = false;
		}
		
		public override function update():void {
			super.update();
			
			if (FlxG.mouse.justPressed() && starOne.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				ratingValue = 1;
				wasUpdated = true;
				add(checkmark);
				
				starOne.loadGraphic(Res.StarFull);
				starTwo.loadGraphic(Res.StarEmpty);
				starThree.loadGraphic(Res.StarEmpty);
				starFour.loadGraphic(Res.StarEmpty);
				starFive.loadGraphic(Res.StarEmpty);
			}
			
			if (FlxG.mouse.justPressed() && starTwo.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				ratingValue = 2;
				updateVote();
				
				starOne.loadGraphic(Res.StarFull);
				starTwo.loadGraphic(Res.StarFull);
				starThree.loadGraphic(Res.StarEmpty);
				starFour.loadGraphic(Res.StarEmpty);
				starFive.loadGraphic(Res.StarEmpty);
			}
			
			if (FlxG.mouse.justPressed() && starThree.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				ratingValue = 1;
				updateVote();
				
				starOne.loadGraphic(Res.StarFull);
				starTwo.loadGraphic(Res.StarFull);
				starThree.loadGraphic(Res.StarFull);
				starFour.loadGraphic(Res.StarEmpty);
				starFive.loadGraphic(Res.StarEmpty);
			}
			
			if (FlxG.mouse.justPressed() && starFour.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				ratingValue = 4;
				updateVote();
				
				starOne.loadGraphic(Res.StarFull);
				starTwo.loadGraphic(Res.StarFull);
				starThree.loadGraphic(Res.StarFull);
				starFour.loadGraphic(Res.StarFull);
				starFive.loadGraphic(Res.StarEmpty);
			}
			
			if (FlxG.mouse.justPressed() && starFive.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				ratingValue = 5;
				updateVote();
				
				starOne.loadGraphic(Res.StarFull);
				starTwo.loadGraphic(Res.StarFull);
				starThree.loadGraphic(Res.StarFull);
				starFour.loadGraphic(Res.StarFull);
				starFive.loadGraphic(Res.StarFull);
			}
			
			if (FlxG.mouse.justPressed() && none.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				ratingValue = 0;
				updateVote();
				
				starOne.loadGraphic(Res.StarEmpty);
				starTwo.loadGraphic(Res.StarEmpty);
				starThree.loadGraphic(Res.StarEmpty);
				starFour.loadGraphic(Res.StarEmpty);
				starFive.loadGraphic(Res.StarEmpty);
			}
		}
		
		public function hadRated():Boolean {
			return wasUpdated;
		}
		
		private function updateVote():void {
			wasUpdated = true;
			add(checkmark);
		}
		
		public function getRatingValue():int {
			return ratingValue;
		}
	}
}